home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / rsynth21.zip / hplay.c < prev    next >
Text File  |  1996-07-19  |  4KB  |  181 lines

  1. #include <config.h>
  2. #include <stdio.h>
  3. #include <float.h>
  4. #include <os2.h>
  5. #define  INCL_OS2MM
  6. #include <os2me.h>
  7. #include "proto.h"
  8. #include "getargs.h"
  9. #include "hplay.h"
  10.  
  11. /* PlayList Entry */
  12.  
  13. typedef struct _PLE {
  14.     ULONG operation;
  15.     ULONG operand1;
  16.     ULONG operand2;
  17.     ULONG operand3;
  18. }PLE;
  19.  
  20. long samp_rate  = 8000;
  21. long bits = 8;
  22. int quiet = FALSE;
  23. int chipmunk = FALSE;
  24. MCI_OPEN_PARMS mop;
  25. PLE playlist[2];
  26.  
  27. void mci_err(ULONG rc)
  28. {
  29.     const rsize = 128;
  30.     char rbuff[rsize];
  31.  
  32.     ULONG rc2 = mciGetErrorString(rc,      // error code
  33.                                   rbuff,   // return buffer
  34.                                   rsize);  // rbuff size
  35.  
  36.     if (rc2 == MCIERR_SUCCESS)
  37.         fprintf(stderr,"MCI error: %s\n\n",rbuff);
  38.     else
  39.         fprintf(stderr,"error # %d has occured!\n\n", rc);
  40. }
  41.  
  42. int
  43. audio_init(int argc, char **argv)
  44. {
  45.  int rate;
  46.  
  47.  rate = 8;
  48.  argc = getargs("Audio Initialization", argc, argv,
  49.                 "r", "%d",  &rate,   "Sample Rate in kHz - 8, 11, 22, or 44",
  50.                 "Q", NULL, &quiet,   "+Q for no sound output (-Q Default)",
  51.             "C", NULL, &chipmunk, "+C if speech sounds like chipmunks",
  52.                 "b", "%d", &bits, "Chunk size of playback sample 8 (def) or 16", 
  53.                 NULL);
  54.  if (help_only)
  55.   return (argc);
  56.  
  57.  switch(rate)
  58.     {
  59.      case  8 : samp_rate =  8000;
  60.                break;
  61.      case 11 : samp_rate = 11025;
  62.                break;
  63.      case 22 : samp_rate = 22050;
  64.                break;
  65.      case 44 : samp_rate = 44100;
  66.                break;
  67.      default : samp_rate =  8000;
  68.     }
  69.  
  70.   if (bits != 16)
  71.      bits = 8;
  72.  
  73.   return (argc);
  74. }
  75.  
  76. void
  77. audio_term(void)
  78. {
  79. }
  80.  
  81. void
  82. audio_play(int n, short *data)
  83. {
  84.  MCI_PLAY_PARMS mpp;
  85.  MCI_GENERIC_PARMS mgp;
  86.  MCI_WAVE_SET_PARMS wsp;
  87.  ULONG rc;
  88.  
  89.  if (!quiet)
  90.   {
  91.    unsigned char *plabuf;
  92.  
  93.    if (bits == 8)
  94.      {
  95.        plabuf = (unsigned char *) malloc(n);
  96.        if (plabuf)
  97.          {
  98.           unsigned char *p = plabuf;
  99.           unsigned char *e = p + n;
  100.           short temp;
  101.           while (p < e)
  102.             {
  103.              temp = *data / 128;
  104.              *p = temp + 128;
  105.              p++;
  106.              data++;
  107.             }
  108.          }
  109.        else 
  110.          {
  111.            fprintf(stderr, "Insufficient memory for Play Buffer\n\n");
  112.            return;
  113.          }
  114.      }
  115.    else
  116.       plabuf = (char *)data;
  117.    
  118.    playlist[0].operation = DATA_OPERATION;
  119.    playlist[0].operand1  = (long) plabuf;
  120.    playlist[0].operand2  = n;
  121.  
  122.    if(bits == 16) playlist[0].operand2 = n*2;
  123.  
  124.    playlist[0].operand3  = 0;
  125.    playlist[1].operation = EXIT_OPERATION;
  126.    mop.hwndCallback   = 0;
  127.    mop.usDeviceID     = 0;
  128.    mop.pszDeviceType  = MCI_DEVTYPE_WAVEFORM_AUDIO_NAME;
  129.    mop.pszElementName = (void *)&playlist[0];
  130.  
  131.    rc = mciSendCommand(0,
  132.                        MCI_OPEN,                        // open message
  133.                        MCI_WAIT | MCI_OPEN_SHAREABLE |  // message flags
  134.                        MCI_OPEN_PLAYLIST,
  135.                        &mop,                            // parameters
  136.                        0);
  137.  
  138.    if (rc != MCIERR_SUCCESS) mci_err(rc);
  139.  
  140.    // set device parameters
  141.    wsp.hwndCallback    = 0;
  142.    if(chipmunk)
  143.       wsp.ulSamplesPerSec = samp_rate/2;
  144.    else
  145.       wsp.ulSamplesPerSec = samp_rate;
  146.    
  147.    wsp.usBitsPerSample = bits;
  148.    rc = mciSendCommand(mop.usDeviceID,
  149.                        MCI_SET,
  150.                        MCI_WAIT |
  151.                        MCI_WAVE_SET_SAMPLESPERSEC |
  152.                        MCI_WAVE_SET_BITSPERSAMPLE,
  153.                        &wsp,
  154.                        0);
  155.  
  156.     if (rc != MCIERR_SUCCESS) mci_err(rc);
  157.     mpp.hwndCallback = 0;
  158.     rc = mciSendCommand(mop.usDeviceID,
  159.                         MCI_PLAY,
  160.                         MCI_WAIT,
  161.                         &mpp,
  162.                         0);
  163.      if (rc != MCIERR_SUCCESS) mci_err(rc);
  164.      
  165.      // close device
  166.  
  167.      mgp.hwndCallback = 0;
  168.      rc = mciSendCommand(mop.usDeviceID,
  169.                         MCI_CLOSE,
  170.                         MCI_WAIT,
  171.                         &mgp,
  172.                         0);
  173.  
  174.      if (rc != MCIERR_SUCCESS) mci_err(rc);
  175.      if (bits == 8)
  176.         free(plabuf);
  177.      _control87(EM_INVALID | EM_DENORMAL | EM_ZERODIVIDE | EM_OVERFLOW | EM_UNDERFLOW | EM_INEXACT, MCW_EM);
  178.  
  179.    }
  180. }
  181.